package com.nervenets.web.controller.handler.admin;

import com.nervenets.web.controller.handler.SessionHandler;
import com.nervenets.web.controller.handler.common.TokenParams;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.enumeration.Role;
import com.nervenets.web.hibernate.domain.user.User;
import com.nervenets.web.hibernate.domain.user.UserRole;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

public class AdminLoginInfoHandler extends SessionHandler<TokenParams> {
    @Override
    public ArrayList<Role> allowRoles() {
        return Role.all();
    }

    @Override
    public boolean needLog() {
        return false;
    }

    @Override
    public String handle(TokenParams params, HttpServletRequest request) throws Exception {
        final User user = userDao.findById(params.getUserId(), User.class);
        if (null == user) return errorMessage(MessageCode.code_404, "账号不存在！");

        return successMessage(new Model(user.getId(), params.getToken(), user.getInfo().getName(), user.getRole(), user.getMobile(), user.getAvatar()));
    }

    @Override
    public String result() throws Exception {
        return successMessage(new Model());
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    class Model {
        private long id;
        private String token = "token";
        private String name = "名称";
        private UserRole role = new UserRole();
        private String mobile = "手机号码";
        private String avatar;
    }
}
